const { Controller } = require("egg");
const { respAdapter } = require("../../utils");

class LogController extends Controller {
  async list() {
    const { ctx, app, config } = this;
    try {
      const token = ctx.headers.authorization
        ? ctx.headers.authorization.split(" ")[1]
        : "";
      await app.jwt.verify(token, config.jwt.secret);
      const query = ctx.query;
      const { event_id } = query;

      const list = await this.app.mysql.select("log", {
        where: {
          event_id
        },
        orders: [
          ["created_time", "DESC"] // 指定时间字段和降序排序
        ]
      });

      if (!list) {
        ctx.body = respAdapter(null, "数据异常，请重试", false);
      }

      ctx.body = respAdapter({ list });
    } catch (err) {
      ctx.body = respAdapter(null, err, false);
    }
  }
}

module.exports = LogController;
